$(function(){
	// 获取用户信息
	getUserInfo({
		success: function(res){
			if (res.code == 0) {
				var data = res.data;
				$(".user-name").text(data.username);
			}else{
				window.location.href = "signin.html";
				// alert(res.msg);
			}
		},
		fail: function(res){
			var res = JSON.parse(res.responseText);
			alert(res.msg)
		}
	})
	// 获取考试列表
	var nowPage = 1, pageSize = 10, searchStatus = -1; searchExamName = '' , searchStartDate = '', searchEndDate = '';
	getExamListFun();
	function getExamListFun(){
		var prama = {
			page: nowPage,
			size: pageSize,
			success: function(res){
				if(res.code === 0){
					setExamList(res,nowPage);
				}else{
					alert(res.msg)
				}	
			},
			fail: function(res){
				var res = JSON.parse(res.responseText);
				alert(res.msg);
			}
		}
		if(searchExamName){
			prama.name = searchExamName
		}
		if(searchStatus > -1){
			prama.status = searchStatus
		}
		if(searchStartDate){
			prama.start_date = searchStartDate
		}
		if(searchEndDate){
			prama.end_date = searchEndDate
		}
		getExamList(prama)
	}
	function setExamList(res,page){
		var results = res.data.results;
		var count = res.data.count;
		var previous = res.data.previous;
		var next = res.data.next;
		var page_count = 10;
		setPagination({
			previous: previous,
			next: next,
			count: count,
			page_count: page_count,
			nowPage: nowPage,
			continueFun: function(){
				$(".exam-tr").remove();
				$(results).each(function(index, item){
					var clone = $(".clone-examtr").clone().removeClass("clone-examtr hide").addClass("exam-tr");
					clone.attr({"data-id":item.code, "data-name": item.name, "data-begin": item.begin_time, "data-end": item.end_time, "data-status": item.status});
					clone.find(".exam-name").text(item.name);
					clone.find(".exam-begin").text(item.begin_time);
					clone.find(".exam-end").text(item.end_time);
					// clone.find(".founder-name").text(item.name);
					clone.find(".founder-time").text(item.create_time);
					clone.find(".exam-status").attr("data-status", item.status).text(getExamStatus(item.status));
					if (item.status != 0) {
						clone.find(".start-exam").remove();
					}
					if (item.status != 1) {
						clone.find(".end-exam").remove();
					}
					if (item.status != 2) {
						clone.find(".derive-exam").remove();
					}
					$("table.exam-table").append(clone);
				})
				$(".hasnot-table").addClass("hide");
				$(".has-table").removeClass("hide");	
			}
		})
	}
	// 点击搜索
	$("#exam_search").on("click", function(e){
		e.stopPropagation();
		searchExamName = $("#search_exam_name").val().trim();
		searchStartDate = $("#search_begin_datetimepicker").val().trim();
		searchEndDate = $("#search_end_datetimepicker").val().trim();
		if(searchStartDate!=""&&searchEndDate!=""){
			var begin = +new Date(searchStartDate);
			var end = +new Date(searchEndDate);
			if(begin >= end){
				alert("开始时间需小于结束时间");
				return
			}
		}
		getExamListFun();
	})
	// 清空搜索
	$("#clear_exam_search").on("click", function(e){
		e.stopPropagation();
		searchExamName = '';
		searchStartDate = '';
	  	searchEndDate = '';
		$("#search_begin_datetimepicker,#search_end_datetimepicker").val("");
		getExamListFun();
	})
	// 点击切换状态tab
	$(".exam-tab-item").on("click", function(e){
		e.stopPropagation();
		var status = parseInt($(this).attr("data-status"));
		$(this).addClass("active").siblings(".exam-tab-item").removeClass('active');
		searchStatus = status;
		getExamListFun();
	})
	// 初始化日历插件
	$('#search_begin_datetimepicker, #search_end_datetimepicker, #create_begin_datetimepicker, #create_end_datetimepicker').datetimepicker({
	    format: 'yyyy-mm-dd hh:ii',
	    autoclose: true
	});
	// 点击创建考试
	$("#create_exam").on("click", function(e){
		e.stopPropagation();
		$("#examModal").attr("data-type","create");
		$("#examModalLabel").text("创建考试");
		$("#examModal input").val("");
		$("#examModal").modal('show');
	})
	// 点击导出考试
	$(document).on("click",".derive_exam", function(e){
		e.stopPropagation();
		var id = $(this).parents("tr").attr("data-id"); 
		var url = "/api/line_up/exam_derive/" + id + "/"
		window.open(url)
	})
	// 点击编辑考试
	$(document).on("click", ".opr-item.edit-item", function(e){
		e.stopPropagation();
		var status = $(this).parents("tr").attr("data-status");
		if(status == 1){
			alert("考试正在进行中，无法编辑");
			return
		}
		if(status == 2){
			alert("考试已结束，无法编辑");
			return
		}
		var id = $(this).parents("tr").attr("data-id");
		var name = $(this).parents("tr").attr("data-name");
		var begin_time = $(this).parents("tr").attr("data-begin");
		var end_time = $(this).parents("tr").attr("data-end");
		$("#examModal").attr({"data-id":id,"data-type": "change"});
		$("#examModalLabel").text("编辑考试");
		$("#exam_name").val(name);
		$("#create_begin_datetimepicker").val(begin_time);
		$("#create_end_datetimepicker").val(end_time);
		$("#examModal").modal('show');
	})
	// 确定创建考试或者确定修改考试信息
	$(document).on("click", "#examModal .modal-sure", function(e){
		e.stopPropagation();
		var _type = $("#examModal").attr("data-type");
		var exam_name = $("#exam_name").val().trim();
		var begin_time = $("#create_begin_datetimepicker").val().trim();
		var end_time = $("#create_end_datetimepicker").val().trim();
		if(exam_name === ""){
			alert("考试名称不能为空");
			return
		}else if(begin_time === ""){
			alert("请先选择开始时间");
			return
		}else if(end_time === ""){
			alert("请先选择结束时间");
			return
		}else if(!compareTime(begin_time, end_time)){
			alert("考试间隔时间需大于等于五分钟");
			return
		}
		if(_type === "create"){
			// 创建考试
			$.ajax({
				url: "/api/line_up/exams/",
				type: "POST",
				dataType: "JSON",
				data: {
					name: exam_name,
					begin_time: begin_time,
					end_time: end_time
				},
				success: function(res){
					if(res.code === 0){
						getExamListFun();
						alert("新建考试成功");
						$("#examModal").modal('hide');
					}else{
						alert(res.msg)
					}
				},
				error: function(res){
					var res = JSON.parse(res.responseText);
					alert(res.msg);
				}
			})
		}else if(_type === "change"){
			// 修改考试
			var code = $("#examModal").attr("data-id");
			$.ajax({
				url: "/api/line_up/exams/"+code+"/",
				type: "PUT",
				dataType: "JSON",
				data: {
					name: exam_name,
					begin_time: begin_time,
					end_time: end_time
				},
				success: function(res){
					if(res.code === 0){
						getExamListFun();
						alert("修改考试成功");
						$("#examModal").modal('hide');
					}else{
						alert(res.msg)
					}
				},
				error: function(res){
					var res = JSON.parse(res.responseText);
					alert(res.msg);
				}
			})
		}
	})
	// 点击删除考试
	$(document).on("click", ".opr-item.delete-item", function(e){
		e.stopPropagation();
		var id = $(this).parents("tr").attr("data-id");
		$("#deleteExamModal").attr("data-id",id).modal("show");
	})
	// 确定删除
	$(document).on("click", "#deleteExamModal .modal-sure", function(e){
		e.stopPropagation();
		var id = $("#deleteExamModal").attr("data-id");
		$.ajax({
			url: "/api/line_up/exams/"+id+"/",
			type: "DELETE",
			dataType: "JSON",
			success: function(res){
				getExamListFun();
				alert("删除考试成功");
				$("#deleteExamModal").modal('hide');
			},
			error: function(res){
				var res = JSON.parse(res.responseText);
				alert(res.msg);
			}
		})
	})
	// 点击开始考试
	$(document).on("click", ".start-exam", function(e){
		e.stopPropagation();
		var id = $(this).parents("tr").attr("data-id");
		var status = $(this).parents("tr").attr("data-status");
		if(status!=0){
			alert("该考试正在进行或者已经结束")
		}else{
			$.ajax({
				url: "/api/line_up/begin_exam/",
				type: "POST",
				data: {
					exam: id
				},
				dataType: "JSON",
				success: function(res){
					if(res.code === 0){
						getExamListFun();
						alert("开始考试成功");
					}else{
						alert(res.msg)
					}
				},
				error: function(res){
					var res = JSON.parse(res.responseText);
					alert(res.msg);
				}
			})
		}
		
	})
	// 点击结束考试
	$(document).on("click", ".end-exam", function(e){
		e.stopPropagation();
		var id = $(this).parents("tr").attr("data-id");
		var status = $(this).parents("tr").attr("data-status");
		if(status == 2){
			alert("该考试已经结束")
		}else{
			$.ajax({
				url: "/api/line_up/end_exam/",
				type: "POST",
				data: {
					exam: id
				},
				dataType: "JSON",
				success: function(res){
					if(res.code === 0){
						getExamListFun();
						alert("结束考试成功");
					}else{
						alert(res.msg)
					}
				},
				error: function(res){
					var res = JSON.parse(res.responseText);
					alert(res.msg);
				}
			})
		}
	})
	// 点击导出考试
	$(document).on("click", ".derive-exam", function(e){
		e.stopPropagation();
		var id = $(this).parents("tr").attr("data-id");
		var url = "/api/line_up/exam_derive/" + id + "/";
		window.open(url);
	})
	// 点击跳转考生管理页面
	$(document).on("click", ".opr-item.view-item", function(e){
		e.stopPropagation();
		var code = $(this).parents("tr").attr("data-id");
		// window.location.href = 'student-manage.html?code='+code;
		localStorage.exam_code = code;
		window.location.href = 'student-manage.html';
	})
	// 上一页
	$(document).on("click", "#previous_page:not(.disabled)", function(e){
		nowPage--;
		getExamListFun();
	})
	// 下一页
	$(document).on("click", "#next_page:not(.disabled)", function(e){
		nowPage++;
		getExamListFun();
	})
	// 选择某一页
	$(document).on("click", ".page-li:not(.disabled):not(.active)", function(e){
		var that = $(this);
		nowPage = parseFloat(that.text().trim()) || 1;
		getExamListFun();
	})
})

